<template>
  <a-drawer
    title="Create a new account"
    :visible="visible"
    :title="title"
    :maskClosable="true"
    width="60%"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :destroyOnClose="true"
  >
    <h2>货主管理</h2>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form"   >
        <a-form-item
          v-show="false"
          label="id"
        >
          <a-input
            :disabled="showable"
            v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          class='form-item-left'
          label="供应商编码"
        >
          <a-input
            placeholder='请输入供应商编码'
            :disabled="showable||editable"
            v-decorator="['suppliersCode', {rules: [{required: true, message: '请输入供应商编码'}]}]" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="供应商名称"

        >
          <a-input
            placeholder='请输入供应商名称'
            :disabled="showable"
            v-decorator="['suppliersName', {rules: [{required: true, message: '请输入供应商名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="材料设备类别"
          class='form-item-left'
        >
          <a-select
            mode="multiple"
            :disabled="showable"
            :options="materialsCategory"
            v-decorator="['materialsTypeCodeList', {rules: [{required: true, message: '请选择所属类别'}]}]" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="统一社会信用代码"
        >
          <a-input
            placeholder='请输入统一社会信用代码'
            :disabled="showable"
            v-decorator="['unifiedSocialCode']" />
        </a-form-item>
        <a-form-item
          label="银行名称"
          class='form-item-left'
        >
          <a-input
            placeholder='请输入银行名称'
            :disabled="showable"
            v-decorator="['bankName']" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="银行账户"
        >
          <a-input
            placeholder='请输入银行账户'
            :disabled="showable"
            v-decorator="['bankId']" />
        </a-form-item>
        <a-form-item
          class='form-item-left'
          label="法人"
        >
          <a-input
            placeholder='请输入法人'
            :disabled="showable"
            v-decorator="['legalPerson']" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="评价等级"
        >
          <a-select
            showSearch
            :disabled="showable"
            :options="rankList"
            allowClear="true"
            :filterOption="filterOption"
            optionFilterProp="children"
            v-decorator="['rank']" />
        </a-form-item>
        <a-form-item
          class='form-item-left'
          label="电话"
        >
          <a-input
            placeholder='请输入电话'
            :disabled="showable"
            v-decorator="['phone']" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="地址"
        >
          <a-input
            placeholder='请输入地址'
            :disabled="showable"
            v-decorator="['place']" />
        </a-form-item>
        <a-form-item

          class='form-item-left'
          label="邮箱"
        >
          <a-input
            placeholder='请输入邮箱'
            :disabled="showable"
            v-decorator="['mailbox']" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="QQ号"
        >
          <a-input
            placeholder='请输入QQ账号'
            :disabled="showable"
            v-decorator="['qq']" />
        </a-form-item>
        <a-form-item
          class='form-item-left'
          label="供应商联系人"
        >
          <a-input
            placeholder='请输入供应商联系人'
            :disabled="showable"
            v-decorator="['contact']" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="供应商电话"
        >
          <a-input
            placeholder='请输入供应商电话'
            :disabled="showable"
            v-decorator="['contactPhone']" />
        </a-form-item>
        <a-form-item
          class='form-item-left'
          label="联系人性别"
        >
          <a-select
            showSearch
            :disabled="showable"
            :options="sexList"
            allowClear="true"
            :filterOption="filterOption"
            optionFilterProp="children"
            v-decorator="['contactSex']" />
        </a-form-item>
        <a-form-item
          class='form-item-right'
          label="状态"
        >
          <a-radio-group v-decorator="['status']"  :disabled="showable" :default-value="0">
            <a-radio :value="0">
              启用
            </a-radio>
            <a-radio :value="1">
              停用
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          class='form-item-left'
          label="备注 "
        >
          <a-textarea
            placeholder='请输入备注'
            :disabled="showable"
            v-decorator="['remarks', {rules: [{required: false, message: '请输入备注'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>

    <div v-show="editable || addable"
         class="drawer-bootom-button">
      <a-button key="cancel" @click="visible = false">取消</a-button>
      <a-button key="forward" :loading="confirmLoading" type="primary" style='margin-left: 15px' @click="handleOk">保存</a-button>
    </div>

  </a-drawer>
</template>

<script>
import { FormMixin } from '@/mixins/FormMixin'
import pick from 'lodash.pick'
import { addObj, getObj, putObj } from '@/api/base/suppliersBase'
import { materialsCategoryDropDown } from '@/api/base/materialsCategoryBase'
import { dropDownByCode } from '@api/sys/sys'


export default {
  name: 'suppliersAddOrUpdate',
  mixins: [FormMixin],
  data() {
    return {
      confirmLoading: false,
      materialsCategory:[],
      rankList:[],
      sexList:[],
      materialsTypeCodeList:[],
      form: this.$form.createForm(this),
      wrapperColUnifiedSocialCode: {
        span: 13
      },
      labelColUnifiedSocialCode: {
        span: 10
      },
      wrapperColSuppliers: {
        span: 15
      },
      labelColSuppliers: {
        span: 8
      },
      visible:false,
      title:null
    }
  },
  methods: {
    edit(id, type) {
      if(type === 'add'){
        this.title = '新建'
      }else if(type === 'edit'){
        this.title = '编辑'
      }else if(type === 'show') {
        this.title = '查看'
      }else{
        this.title = null
      }
      this.visible = true
      //评价等级
      dropDownByCode('rank').then(res => {
        this.rankList = res.result
      })
      //联系人性别
      dropDownByCode('contact_sex').then(res => {
        this.sexList = res.result
      })
      materialsCategoryDropDown().then(res=>{
        this.materialsCategory = res.data
      })
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          if (record.category === 'lineSite') {
            this.typeChange(record.category)
          }
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'suppliersCode', 'suppliersName','bankName','bankId','phone','rank', 'materialsTypeCodeList','unifiedSocialCode','qq','mailbox','contact','contactPhone','contactSex', 'legalPerson','place', 'remarks', 'status']))
          })
        })
      } else {
        resetFields()
      }
    },
    // 过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },

    //点击保存，执行的方法
    handleOk() {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true

          if (this.type === 'add') {
            addObj(values).then(res => {
              if (res.code !== 200 && res.code !== 0) {
                this.confirmLoading = false
                this.$message.error(res.message)
              } else {
                this.confirmLoading = false
                this.$message.success('操作成功')
                this.$emit('ok')
                this.handleCancel()
              }
            })
          } else if (this.type === 'edit') {
            putObj(values).then(res => {
              if (res.code !== 200 && res.code !== 0) {
                this.confirmLoading = false
                this.$message.error(res.message)
              } else {
                this.confirmLoading = false
                this.$message.success('操作成功')
                this.$emit('ok')
                this.handleCancel()
              }

            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.ant-advanced-search-form {
}
.form-item-left{
  width:48%;
  display: inline-block;

}
.form-item-right{
  width:48%;
  display: inline-block;
  float: right;
}
 .drawer-bootom-button {
   position: absolute;
   bottom: -8px;
   width: 100%;
   border-top: 1px solid #e8e8e8;
   padding: 10px 16px;
   text-align: right;
   left: 0;
   background: #fff;
   border-radius: 0 0 2px 2px;
 }
</style>